<template>
  <div>
    <el-button type="primary" @click="dialogFormVisible = true">
        添加
    </el-button>

    <el-dialog title="添加信息" :visible.sync="dialogFormVisible">
      <el-form :model="form">
        <el-form-item label="名称" label-width="120px">
          <el-input v-model="form.name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="缩写" label-width="120px">
          <el-input v-model="form.short" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="数量" label-width="120px">
          <el-input v-model.number="form.count" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="addItem">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
// 通过promise的方式对ajax进行了二次封装
import axios from "../http"
export default {
  props: ['init'],
  data() {
    return {
    //   控制显示隐藏
      dialogFormVisible: false,
      form: {
        name: "",
        short: "",
        count: null
      }
    }
  },
  methods: {
      addItem() {
        axios.post('/api/users/addItem', {
            ...this.form
        }).then(res => {
            console.log(res)
            if (res.data.code === 1) {
                this.$message.success(res.data.message)
                this.init()
            } else {
                this.$message.error(res.data.message)
            }
        })
        this.dialogFormVisible = false
      }
  }
};
</script>

<style>
</style>